﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CSharpGDIDraw
{
    public partial class FrmZoomingAndPanning : Form
    {
        public FrmZoomingAndPanning()
        {
            InitializeComponent();

            CreateWorkGraphics();
        }
        /// <summary>
        /// 创建画布
        /// </summary>
        public void CreateWorkGraphics()
        {
            if (bottomMapGraphics != null)
                bottomMapGraphics.Dispose();
            if (workMapGraphics != null)
                bottomMapGraphics.Dispose();
            if (viewGraphics != null)
                viewGraphics.Dispose();
            BufferedGraphicsContext bgc = BufferedGraphicsManager.Current;
            viewGraphics = this.panelWorkArea.CreateGraphics();
            viewGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

            workMapGraphics = bgc.Allocate(viewGraphics, this.panelWorkArea.DisplayRectangle);
            workMapGraphics.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

            bottomMapGraphics = bgc.Allocate(viewGraphics, this.panelWorkArea.DisplayRectangle);
            bottomMapGraphics.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;


            this.panelWorkArea.MouseWheel += panelWorkArea_MouseWheel;
        }
        /// <summary>
        /// 
        /// </summary>
        private SizeF ZoomScale = new SizeF(1.0F, 1.0F);
        /// <summary>
        /// 鼠标滚轮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void panelWorkArea_MouseWheel(object sender, MouseEventArgs e)
        {
            if (e.Delta > 0)
            {
                ZoomScale = new SizeF(ZoomScale.Width * 1.2F, ZoomScale.Height * 1.2F);
                this.panelWorkArea.Size = new Size((int)(panelWorkArea.Size.Width * 1.2F), (int)(panelWorkArea.Size.Height*1.2F));
            }
            else
            {
                ZoomScale = new SizeF(ZoomScale.Width * 0.8F, ZoomScale.Height * 0.8F);
                this.panelWorkArea.Size = new Size((int)(panelWorkArea.Size.Width * 0.8F), (int)(panelWorkArea.Size.Height * 0.8F));
                
            }
            this.panelWorkArea.Invalidate();
        }
        /// <summary>
        /// 获取顶层显示画布 == (对选中元素的操作)
        /// </summary>
        private Graphics viewGraphics = null;
        /// <summary>
        /// 底层元素图像
        /// </summary>
        private BufferedGraphics bottomMapGraphics = null;
        /// <summary>
        /// 工作元素图像
        /// </summary>
        private BufferedGraphics workMapGraphics = null;
        /// <summary>
        /// 工作区绘制
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmZoomingAndPanning_Paint(object sender, PaintEventArgs e)
        {

        }

        private void DrawWork()
        {


            // 蛋疼地说为毛用 BufferedGraphics 就花屏

            bottomMapGraphics.Graphics.Clear(Color.LightBlue);
            // Set world transform of graphics object to rotate.
            bottomMapGraphics.Graphics.RotateTransform(30.0F);

            // Then to scale, prepending to world transform.
            bottomMapGraphics.Graphics.ScaleTransform(3.0F, 1.0F);
            
            //e.Graphics.ScaleTransform(ZoomScale.Width, ZoomScale.Height);
            //bottomMapGraphics.Graphics.FillRectangle(Brushes.CornflowerBlue, this.panelWorkArea.DisplayRectangle);
            bottomMapGraphics.Graphics.DrawString("卷了头发的__May", new Font("华文新魏", 12), Brushes.DarkKhaki, new PointF(50, 50));
            // Draw scaled, rotated rectangle to screen.
            bottomMapGraphics.Graphics.DrawRectangle(new Pen(Color.Blue, 3), 50, 0, 100, 40);
             
             
            bottomMapGraphics.Render();

           
             
        }

        private void panelWorkArea_Paint(object sender, PaintEventArgs e)
        {
            DrawWork(e.Graphics);

          
            

        }

        private void DrawWork(Graphics graphics)
        {
            graphics.Clear(Color.LightBlue);
            // Set world transform of graphics object to rotate.
            graphics.RotateTransform(30.0F);

            // Then to scale, prepending to world transform.
            //graphics.ScaleTransform(3.0F, 1.0F);

            graphics.ScaleTransform(ZoomScale.Width, ZoomScale.Height);
            //bottomMapGraphics.Graphics.FillRectangle(Brushes.CornflowerBlue, this.panelWorkArea.DisplayRectangle);
            graphics.DrawString("卷了头发的__May", new Font("华文新魏", 12), Brushes.DarkKhaki, new PointF(50, 50));
            // Draw scaled, rotated rectangle to screen.
            graphics.DrawRectangle(new Pen(Color.Blue, 3), 50, 0, 100, 40);


            graphics.Flush();
        }

        private void FrmZoomingAndPanning_Load(object sender, EventArgs e)
        {

        }
        Point lastMousePos = Point.Empty;
        /// <summary>
        /// 鼠标事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmZoomingAndPanning_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                lastMousePos = e.Location;
            }
            else if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                this.panelWorkArea.Focus();
            }
        }

        private void FrmZoomingAndPanning_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                Point newMousePos = e.Location;
                if (newMousePos != lastMousePos)
                {
                    Size moveSize = new Size(newMousePos.X - lastMousePos.X, newMousePos.Y - lastMousePos.Y);

                    panelWorkArea.Location = panelWorkArea.Location + moveSize;
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmZoomingAndPanning_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                lastMousePos = Point.Empty;
            }
        }
    }
}
